package com.whq.mvc.controller;

import com.whq.mvc.resp.BusinessException;
import com.whq.mvc.resp.CommonReturnType;
import com.whq.mvc.resp.EmBusinessError;
import com.whq.mvc.service.UserService;
import com.whq.mvc.service.model.UserModel;
import com.whq.mvc.utils.Md5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping(value = "/getotp")
    public CommonReturnType getOtp(@RequestParam(name = "telphone") String telphone, HttpServletRequest request) {
        //需要按照一定的规则生成OTP验证码
        Random random = new Random();
        int randomInt = random.nextInt(99999);
        randomInt += 10000;
        String otpCode = String.valueOf(randomInt);
        //将OTP验证码同对应用户的手机号关联，使用httpsession的方式绑定他的手机号与OTPCODE
        request.getSession().setAttribute(telphone, otpCode);
        //将OTP验证码通过短信通道发送给用户,省略
        System.out.println("telphone = " + telphone + " & otpCode = " + otpCode);
        return CommonReturnType.create(null);
    }

    @PostMapping("/login")
    public CommonReturnType login(@RequestParam(name = "telphone") String telphone,
                                  @RequestParam(name = "password") String password, HttpServletRequest request) throws BusinessException {
        if (StringUtils.isEmpty(telphone) || StringUtils.isEmpty(password)) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        UserModel userModel = userService.validateLogin(telphone, Md5Util.encodeByMd5(password));
        //request.getSession().setAttribute("IS_LOGIN", true);
        //request.getSession().setAttribute("LOGIN_USER", userModel);
        //将cookie改为token
        String uuidToken = UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set(uuidToken, userModel, 30, TimeUnit.MINUTES);
        return CommonReturnType.create(uuidToken);
    }

    @PostMapping("/register")
    public CommonReturnType register(@RequestParam(name = "telphone") String telphone,
                                     @RequestParam(name = "otpCode") String otpCode,
                                     @RequestParam(name = "name") String name,
                                     @RequestParam(name = "gender") Integer gender,
                                     @RequestParam(name = "age") Integer age,
                                     @RequestParam(name = "password") String password, HttpServletRequest request) throws BusinessException {
        String verifyCode = (String) request.getSession().getAttribute(telphone);
        if (!otpCode.equals(verifyCode)) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "短信验证码不正确");
        }
        UserModel userModel = new UserModel();
        userModel.setName(name);
        userModel.setGender(gender.byteValue());
        userModel.setAge(age);
        userModel.setTelphone(telphone);
        userModel.setRegisterMode("phone");
        userModel.setEncrptPassword(Md5Util.encodeByMd5(password));
        userService.register(userModel);
        return CommonReturnType.create(null);
    }
}
